package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.MemberMamaDO;
import com.bootdo.wx.service.MemberMamaService;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;

/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-06-09 16:06:43
 */
 
@Controller
@RequestMapping("/wx/memberMama")
public class MemberMamaController {
	@Autowired
	private MemberMamaService memberMamaService;
	
	@GetMapping()
	@RequiresPermissions("wx:memberMama:memberMama")
	String MemberMama(){
	    return "wx/memberMama/memberMama";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:memberMama:memberMama")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
		//查询列表数据
        Query query = new Query(params);
        if(params.containsKey("name")&&params.get("name")!=null&&!"".equals(params.get("name").toString())){
        	List<MemberMamaDO> memberMamaList = memberMamaService.likeList(query);
        	int total =memberMamaList.size();
    		PageUtils pageUtils = new PageUtils(memberMamaList, total);
    		return pageUtils;
        }
		List<MemberMamaDO> memberMamaList = memberMamaService.list(query);
		int total = memberMamaService.count(query);
		PageUtils pageUtils = new PageUtils(memberMamaList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:memberMama:add")
	String add(){
	    return "wx/memberMama/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:memberMama:edit")
	String edit(@PathVariable("id") String id,Model model){
		MemberMamaDO memberMama = memberMamaService.get(id);
		model.addAttribute("memberMama", memberMama);
	    return "wx/memberMama/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:memberMama:add")
	public R save( MemberMamaDO memberMama){
	    memberMama.setId(UUIDUtils.getUUID());
	    memberMama.setDelFlag(0);
	    memberMama.setCaAt(new Date());
	    if(memberMama.getDays()==null){
	    	return R.error("请填写天数");
	    }
	    Map<String, Object> map = new HashMap<>();
	    map.put("name", memberMama.getName());
	    map.put("delFlag","0");
	    if(memberMamaService.list(map).size()>0){
	    	return R.error("名字不能重复");
	    }
		if(memberMamaService.save(memberMama)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:memberMama:edit")
	public R update( MemberMamaDO memberMama){
		memberMama.setUpAt(new Date());
		 Map<String, Object> map = new HashMap<>();
		map.put("name", memberMama.getName());
	    map.put("delFlag","0");
	    if(memberMamaService.list(map).size()>1){
	    	return R.error("名字不能重复");
	    }
		memberMamaService.update(memberMama);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:memberMama:remove")
	public R remove( String id){
		MemberMamaDO memberMama = memberMamaService.get(id);
		memberMama.setDelFlag(1);
		memberMama.setUpAt(new Date());
		if(memberMamaService.update(memberMama)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:memberMama:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MemberMamaDO memberMama = memberMamaService.get(string);
				memberMama.setDelFlag(1);
				memberMama.setUpAt(new Date());
				memberMamaService.update(memberMama);
			}
		}
	
		return R.ok();
	}
	
}
